package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Model.User;

public class UserDao {
	BaseDao bd=new BaseDao();
	public List<User> getByName(String u_name){
		List<User> ls=new ArrayList();
		User u=new User();
		Connection con=bd.getConnection();
		try {
		String sql="SELECT * FROM `20170726bank`.`user` where u_name=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setString(1, u_name);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				u.setU_id(rs.getInt("u_id"));
				u.setU_name(rs.getString("u_name"));
				u.setU_pwd(rs.getString("u_pwd"));
				ls.add(u);
			}
			bd.closequery(rs, ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ls;
	}
	public boolean addUser(User u){
		boolean flag=false;
		Connection con=bd.getConnection();
		try {
		String sql="insert into user(u_name,u_pwd)values(?,?)";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setString(1, u.getU_name());
			ps.setString(2, u.getU_pwd());
			if(ps.executeUpdate()>0){
				flag=true;
			}
			bd.closeUpdate(ps, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
}
